package util.ranker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Map.Entry;

import util.hashing.Sorting;
import util.io.FileInput;

public class SuggestionsFromFile implements RankingService {

	/**
	 * @param args
	 */
	
	
	private Hashtable<String,Hashtable<String,Float>> suggestions = new  Hashtable<String,Hashtable<String,Float>>();
	public SuggestionsFromFile(String path) throws FileNotFoundException{
	
	
		File directory = new File(path);
		
		if(directory.isDirectory()){
			
			String[] list = directory.list();
			for(int i =0; i<list.length; i++){
				FileReader fr = new FileReader (path+"/"+list[i]);
				BufferedReader br = new BufferedReader(fr);
				Scanner scan = new Scanner(br); 
				
				while(scan.hasNextLine()){
					
					
					String line = scan.nextLine();
					String t[]= line.split("\t");
					String query = t[0];
					if(t.length<3){
				
					continue;
					}
					String suggestion = t[1];
					String rank = t[2];
					
					
					updateSuggestionsHash(query, suggestion, Float.valueOf(rank));
					
				}
				
				scan.close();
				
			}
		}else if(directory.isFile()){
			FileInput in =new FileInput(path);
			String line  = in.readString();
			while(line!=null){
		

					String t[]= line.split("\t");
					String query = t[0];
					if(t.length<3){
						line = in.readString();
					continue;
					}
					String suggestion = t[1];
					String rank = t[2];
					
					//System.out.println(query + "\t"+ suggestion + "\t"+ rank);
					updateSuggestionsHash(query, suggestion, Float.valueOf(rank));
					
					line =in.readString();
			}
				
			
				
		
		}
	
	}
	private void updateSuggestionsHash(String query, String s, Float rank) {
		// TODO Auto-generated method stub
		
		
		if(suggestions.containsKey(query)){
			
			Hashtable<String,Float> sugg = suggestions.get(query);
			sugg.put(s, rank);
		
			suggestions.put(query, sugg);
			
		}else{
			
			Hashtable<String, Float> sugg= new Hashtable<String,Float>();
		
			sugg.put(s, rank);
			suggestions.put(query, sugg);
		}
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	@Override
	public ArrayList<Entry<String, Float>> rankQuery(
	 String query, int limit) {
		// TODO Auto-generated method stub
		
	
		
		if(suggestions.containsKey(query)){
			Hashtable<String, Float> query_hash = suggestions.get(query);
			
			Iterator<Entry<String, Float>> tuples = Sorting.sortHashNumericValues(query_hash, true);
			
			ArrayList<Entry<String,Float>> r = new ArrayList<Entry<String,Float>>();
			
			int i =0;
			System.out.println();
			while(tuples.hasNext() && i<limit){
				
				
				Entry<String, Float> t = tuples.next();
			//	System.out.println(query  + "\t"+ t.getKey() + "\t"+ t.getValue());
				r.add(t);
				
			}
		
			return r;
			
		}else{
			
			return null;
		}
		
	}
	@Override
	public ArrayList<Entry<String, Float>> rankQuery(String query,
			Hashtable<String, Float> q, int limit) {
		rankQuery( query,  limit);
		
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ArrayList<Entry<String, Float>> rankQuery(String query,
			Hashtable<String, Float> q, Hashtable<Short, Float> q_topics,
			int limit) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
